package eMdP.inputHandler.accions.EMdPHandler;


import com.jme.input.action.InputActionEvent;
import com.jme.input.action.KeyInputAction;
import com.jme.math.Vector3f;
import com.jme.util.Timer;

import eMdP.classesBasiques.Personatge;
import eMdP.inputHandler.EMdPHandler;

/**
 * 
 * <code>ThirdPersonForwardAction</code>
 * 
 * @author Joshua Slack
 * @version $Revision: 4131 $
 */
public class EndavantEMdPAction extends KeyInputAction {
    private Vector3f rot;

    private EMdPHandler handler;
    
    private Personatge personatge;

    /**
     * Constructor creates a new <code>ThirdPersonForwardAction</code> object.
     * During construction, the character to direct and the speed at which to
     * move the character is set.
     * 
     * @param handler
     *            the associated handler
     * @param speed
     *            the speed at which the target can move forward
     */
    public EndavantEMdPAction(EMdPHandler handler, float speed) {
        this.handler = handler;
        this.speed = speed;
        rot = new Vector3f();
        
        personatge = ((Personatge)handler.getJugador());
    }

    /**
     * <code>performAction</code> moves the node along it's positive
     * direction vector at a speed of movement speed * time. Where time is
     * the time between frames and 1 corresponds to 1 second.
     * @see com.jme.input.action.InputActionInterface#performAction(InputActionEvent)
     */
    public void performAction(InputActionEvent event) {
    	// Si el jugador està bloquejat, no fem res
    	if(!((Personatge)handler.getJugador()).estaBloquejat(Timer.getTimer().getTimeInSeconds())){
	        if (handler.getPermitter() != null
	                && !handler.getPermitter().canBeMoved())
	            return;
	        handler.setGoingForward(true);
	        Vector3f loc = handler.getTarget().getLocalTranslation();
	        if (handler.isCameraAlignedMovement()) {
	            rot.set(handler.getCamera().getDirection());
	            rot.y = 0;
	        } else {
	            handler.getTarget().getLocalRotation().getRotationColumn(2, rot);
	        }
	        rot.normalizeLocal();
	        loc.addLocal(rot.multLocal((this.speed * event.getTime())));
	        if(!personatge.estaCaminant())
	        {
	        	personatge.posaCaminant();
	        }
    	}
    }
}
